package com.boxin.ims.modules.wechat.activity;

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.boxin.ims.modules.recharge.entity.Recharge;
import com.boxin.ims.modules.recharge.entity.RechargeLog;
import com.boxin.ims.modules.recharge.service.RechargeLogService;
import com.boxin.ims.modules.recharge.service.RechargeService;
import com.boxin.ims.modules.wechat.entity.LotteryLog;
import com.boxin.ims.modules.wechat.entity.LotteryRecord;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.entity.activity.ActivityConfig;
import com.boxin.ims.modules.wechat.service.LotteryLogService;
import com.boxin.ims.modules.wechat.service.LotteryRecordService;
import com.boxin.ims.modules.wechat.service.WechatDaoHelper;
import com.boxin.ims.modules.wechat.service.WechatMemberService;
import com.boxin.ims.modules.wechat.service.activity.ActivityConfigService;
import com.boxin.ims.modules.wechat.utils.ScoreUtils;

public abstract  class ActivityProcessor extends ActivityConfig {
	
	protected final Logger logger = Logger.getLogger(this.getClass());
	
	protected WechatMember member;	//会员
	protected Map<String, Object> param;
	
	
	@Autowired
	ActivityConfigService activityConfigService;
	
	@Autowired
	LotteryRecordService lotteryRecordService;
	
	@Autowired
	RechargeLogService rechargeLogService;
	
	@Autowired
	RechargeService rechargeService;
	
	@Autowired
	WechatMemberService wechatMemberService;
	
	@Autowired
	WechatDaoHelper wechatDaoHelper;
	
	@Autowired
	LotteryLogService lotteryLogService;
	
	public ActivityProcessor() {
		
	}
	
	/**
	 * 创建奖品
	 *@author xiaoqian
	 * 2014-4-11上午11:54:21
	 */
	public Boolean createLotteryRecord(){
		if(lottery!=null&&lottery.getId()>0){
			
			LotteryRecord lotteryRecord = new LotteryRecord();
			lotteryRecord.setUser(user);
			lotteryRecord.setLottery(lottery);
			lotteryRecord.setMember(member);
			lotteryRecord.setStatus(LotteryRecord.STATUS_NORMAL);
			lotteryRecord.setSourceType(LotteryRecord.SOURCE_TYPE_ME);
			lotteryRecord.setScope(LotteryRecord.SCOPE_NEW);
			lotteryRecord.setEndTime(com.boxin.framework.base.utils.DateUtils.getDelayDate(validDays));
			lotteryRecord.setName(lottery.getName());
			
			
			lotteryRecordService.save(lotteryRecord);
			
			LotteryLog lotteryLog =new LotteryLog();
			lotteryLog.setUser(user);
			lotteryLog.setMember(member);
			lotteryLog.setName(lottery.getName());
			lotteryLog.setRemarks("活动处理器奖励");
			
			lotteryLogService.save(lotteryLog);
			
			ActivityConfig activityConfig= activityConfigService.get(id);
			activityConfig.setLotteryNumber(activityConfig.getLotteryNumber()-1);
			activityConfigService.save(activityConfig);
			return true;
		}else{
			return false;
		}
	}
	
	
	/**
	 * 创建奖品
	 *@author xiaoqian
	 * 2014-4-11上午11:54:21
	 */
	public LotteryRecord createLotteryRecordByAnswer(Long id,Long questionnaireId){
		ActivityConfig activityConfig= activityConfigService.get(id);
		if(lottery!=null&&lottery.getId()>0&&activityConfig!=null&&activityConfig.getLotteryNumber()>0){
			
			LotteryRecord lotteryRecord = new LotteryRecord();
			lotteryRecord.setUser(user);
			lotteryRecord.setLottery(lottery);
			lotteryRecord.setMember(member);
			lotteryRecord.setStatus(LotteryRecord.STATUS_NORMAL);
			lotteryRecord.setSourceType(LotteryRecord.SOURCE_TYPE_ME);
			lotteryRecord.setScope(LotteryRecord.SCOPE_NEW);
			lotteryRecord.setEndTime(com.boxin.framework.base.utils.DateUtils.getDelayDate(validDays));
			lotteryRecord.setName(lottery.getName());
			
			
			lotteryRecordService.save(lotteryRecord);
			
			LotteryLog lotteryLog =new LotteryLog();
			lotteryLog.setUser(user);
			lotteryLog.setMember(member);
			lotteryLog.setName(lottery.getName());
			lotteryLog.setRemarks("活动处理器奖励_"+questionnaireId);
			
			lotteryLogService.save(lotteryLog);
			
			
			activityConfig.setLotteryNumber(activityConfig.getLotteryNumber()-1);
			activityConfigService.save(activityConfig);
			return lotteryRecord;
		}else{
			return null;
		}
	}
	
	/**
	 * 活动添加积分
	 *@author xiaoqian
	 * 2014-4-11上午11:58:58
	 */
	public Boolean addScore(){
		if(score!=null){
			ScoreUtils.addScore(member, score, "0",name+"活动奖励"+score/1000d+"个活动积分");
			return true;
		}else{
			return false;
		}
	}
	
	
	public Boolean addMoney(){
		if(money!=null&&money!=0){
			
			if(member !=null&&user!=null ){
				Integer leftMoney = member.getMoney();				//当前余额
				leftMoney = (leftMoney==null)?0:leftMoney;		
				money= (money==null)?0:money;		
				
				Integer cmoney = money;			//充值余额
				Integer tmoney = leftMoney+cmoney;					//充值后总金额
				
				
				//查找会员
				Recharge recharge = rechargeService.getRechargeByUserIdAndNumber(user.getId(), member.getNumber());	
				//存在则更新
				if(recharge != null){
					recharge.setMoney(tmoney);
					recharge.setName(member.getName());
					rechargeService.save(recharge);
					member.setMoney(tmoney);
					wechatMemberService.save(member);
					
				}else{
					return false;
				}
				
				//添加充值日志  流水
				RechargeLog  rechargeLog = new RechargeLog();
				rechargeLog.setMember(member);
				rechargeLog.setUser(user);
				rechargeLog.setName(member.getName());
				rechargeLog.setMoney(cmoney);
				rechargeLog.setRecharge(recharge);
				rechargeLog.setType(RechargeLog.TYPE_RECHARGE);
				rechargeLog.setRemarks("充值"+cmoney/100+"元.");
				String serialNo = wechatDaoHelper.generateRechargeSerialNumber(user.getId());
				rechargeLog.setNumber(member.getNumber());
				rechargeLog.setSerialNo(serialNo);
				rechargeLogService.save(rechargeLog);
				
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	
	
	public Boolean award(){
		if(addScore()==false){
			logger.debug("积分添加失败");
			return false;
		}
		if(createLotteryRecord()==false){
			logger.debug("奖品添加失败");
			return false;
		}
		if(addMoney()==false){
			logger.debug("金钱添加失败");
			return false;
		}
		return true;
	}

	
	public abstract void process();
	
	public abstract LotteryRecord answerProcess();

	public WechatMember getMember() {
		return member;
	}

	public void setMember(WechatMember member) {
		this.member = member;
	}

	public Map<String, Object> getParam() {
		return param;
	}

	public void setParam(Map<String, Object> param) {
		this.param = param;
	}
	
	public void setActivityConfig(ActivityConfig activityConfig){
		this.id=activityConfig.getId();
		this.user=activityConfig.getUser();
		this.name=activityConfig.getName();
		this.memberType=activityConfig.getMemberType();
		this.groupName=activityConfig.getGroupName();
		this.startTime=activityConfig.getStartTime();
		this.endTime=activityConfig.getEndTime();
		this.terminal=activityConfig.getTerminal();
		this.lottery=activityConfig.getLottery();
		this.validDays=activityConfig.getValidDays();
		this.money=activityConfig.getMoney();
		this.score=activityConfig.getScore();
		this.activityProcessor=activityConfig.getActivityProcessor();
		this.status=activityConfig.getStatus();
		this.createTime=activityConfig.getCreateTime();
	}
	
}
